﻿

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <link href="~/Content/layui/css/layui.css" rel="stylesheet" />
    <script src="~/Content/layui/layui.js"></script>
    <script src="~/Content/myjs/jquery-1.10.2.min.js"></script>
</head>
<body>
    <div class="layui-col-md8">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
            <legend>正在为： @ViewBag.userid 配置角色信息</legend>
        </fieldset>
        <div class="layui-row">
            <table id="mytable" lay-filter="test"></table>
        </div>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-row">
                <div class="layui-btn-container layui-col-md6">
                    <button class="layui-btn layui-btn-sm" lay-event="config"><i class="layui-icon"></i>确认分配角色</button>
                </div>
            </div>
        </script>
    </div>

    <script>

        var table;
        var active;
        var url = "";
        var action;

        layui.use('table', function () {
            table = layui.table;
            var form = layui.form;
            //第一个实例
            table.render({
                elem: '#mytable'
              , skin: 'line' //行边框风格
              , url: '/Role/GetJsonList1' //数据接口
              , toolbar: '#toolbarDemo'
              , cellMinWidth: 50    //  自适应宽度，最短宽度80
              , page: false //关闭分页

              , cols: [[ //表头
                { type: 'checkbox', fixed: 'left' }
                , { field: 'RoleId', title: '角色Id', sort: false, fixed: 'left', width:50 }
                , { field: 'RoleName', title: '角色名称', sort: false }
                , { field: 'Description', title: '角色描述', sort: false }
              ]],

            });


            //工具栏事件
            table.on('toolbar(test)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'config':
                        //获取选中的个数
                        if (checkStatus.data.length > 0) {
                            //获取选中的信息
                            var data = checkStatus.data;
                            //转成json格式的字符串
                            var strdata = JSON.stringify(data);
                            //转成json格式
                            var jsondata = JSON.parse(strdata);
                            //构造字符串
                            var idstr = "";
                            for (var i = 0; i < jsondata.length; i++) {
                                idstr += jsondata[i]["RoleId"] + ",";
                            }
                            //调用删除方法
                            btn_configrole(idstr);

                        } else {
                            alert("请选择你要配置的角色！");
                        }
                        break;
                };
            });
        });
        //配置角色
        btn_configrole = function (idstr) {
            //alert(idstr);
            $.ajax({
                type: 'post',
                url: '/Role/ConfigRole',
                data: {
                    arrRoleId: idstr,
                    userId:"@ViewBag.userid",
                },
                success: function (data) {
                    layer.alert(data);
                    btn_search();
                }
            });
        }

    </script>

</body>
</html>
